﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WSPDocGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintOut.ApplicationInfo();

            if (args.Length < 2)
            {
                PrintOut.Usage();
                return;
            }

            string wspFolder = args[0];
            string outputFolder = args[1];

            // Verify input parameters
            if (!Directory.Exists(wspFolder))
            {
                PrintOut.Message("Parameter: 'WSP folder' was not found or access denied!");
                return;
            }

            if (!Directory.Exists(outputFolder))
                Directory.CreateDirectory(outputFolder);

            try
            {
                // Create new WSP folder
                DirectoryInfo dirInfo = new DirectoryInfo(wspFolder);
                if (Directory.Exists(outputFolder))
                {
                    // Delete files (change attributes for read-only files)
                    string[] files = Directory.GetFiles(outputFolder, "*.*", SearchOption.AllDirectories);
                    foreach (string file in files)
                    {
                        File.SetAttributes(file, FileAttributes.Normal);
                    }
                    Directory.Delete(outputFolder, true);
                }

                // Wait before re-create the folder
                System.Threading.Thread.Sleep(0);
                DirectoryInfo wspFolderInfo = Directory.CreateDirectory(outputFolder);

                // Query .WSP files
                var wspFiles = from FileInfo fileinfo in dirInfo.GetFiles()
                               where fileinfo.Extension == ".wsp"
                               select fileinfo;

                foreach (FileInfo file in wspFiles)
                {
                    // Open each file into WSP folder and rename it .cab
                    FileInfo newFile = file.CopyTo(outputFolder + "\\" + file.Name, true);
                    string cabFile = newFile.FullName.Replace(".wsp", ".cab");
                    newFile.MoveTo(cabFile);

                    // Extract cab
                    ExtractWSP.ExpandCabFile(cabFile, outputFolder);
                }

                StringBuilder output = new StringBuilder();
                IHelpGenerator generator = new HtmlHelpGeneratorProvider();

                // Every folder is an WSP extracted 
                foreach (DirectoryInfo file in wspFolderInfo.GetDirectories())
                {
                    // Generate documentation
                    PrintOut.Message(file.Name.Replace(".cab", ""));
                    output.Append(PackageReader.Execute(file.FullName, generator));
                }

                // Save one html with all WSP documentation
                string globalHelpFile = string.Format("{0}\\{1}.{2}",
                    outputFolder, "WSP Documentation", generator.Extension);
                File.Delete(globalHelpFile);
                File.AppendAllText(globalHelpFile, output.ToString());

                PrintOut.BlankLine();
                PrintOut.Message("-----> Operation completed!");
                PrintOut.BlankLine();
            }
            catch (Exception ex)
            {
                PrintOut.Message("An error is occured: {0}. Stack trace: {1}", true, new object[]{ex.Message, ex.StackTrace});
            }

        }

       
    }
}
